﻿using System;
using System.Linq;
using Loki.Game;
using Loki.Game.Objects;
using Loki.TreeSharp;
using Loki.Bot.v2;

namespace GetOutOfAmbushChests
{
    public sealed class MainLogic : ICoroutines
    {
        /// <summary>
        /// Ctor of the Class
        /// </summary>
        #region MainLogic

        public MainLogic() { }

        #endregion

        /// <summary>
        /// Returns the Combatroutine
        /// </summary>
        #region CombatBehavior
        private PrioritySelector CombatBehavior
        {
            get
            {
                return new PrioritySelector(new[] { Loki.Bot.RoutineManager.Current.Buff, Loki.Bot.RoutineManager.Current.Combat });
            }
        }
        #endregion

        /// <summary>
        /// Defines the Owner of this Bot
        /// </summary>
        #region Bot

        public Main Bot ;

        public LocalPlayer MyPlayer;

        #endregion

        private Boolean _interactIsDone = false;
        private Loki.Utilities.Vector2i _myActualPosition;

        public override bool MoveNext()
        {
            switch (Action)
            {
                case 0: // ?
                    Action = -1;
                    break;
                case 1: // Don't need combat
                    Action = -1;
                    goto End;
                case 2: // Need Combat
                    Action = -1;
                    Function = LokiCoroutine.CompositeExecutor(CombatBehavior);
                    Action = 1;
                    return true;
            }

            NetworkObject myStrongBox = LokiPoe.ObjectManager.Objects.First(no => no.Name.Contains("Strongbox"));

            if (myStrongBox != null &&
                myStrongBox.IsValid &&
                myStrongBox.Distance <= 60)
            {

                if (!_interactIsDone)
                {
                    _myActualPosition = MyPlayer.Position;
                    Function = Coroutines.InteractWithObject(myStrongBox);
                    _interactIsDone = true;
                    Action = 1;
                    return true;
                }

                Function = Coroutines.ClickToMoveTo(_myActualPosition);
                Action = 1;
                return true;

            }

        End:
            Function = LokiCoroutine.EndTick;
            Action = 0;
            return true;
        }
    }
}
